(function (vc) {
    vc.extends({
        data: {
            editStoreAdsInfo: {
                adId: '',
                adName: '',
                adTypeCd: '',
                seq: '',
                startTime: '',
                endTime: '',
                pageUrl: '',
                imgUrl: '',
                adTypes: []
            }
        },
        _initMethod: function () {
            vc.initDateTime('editStartTime', function (_value) {
                $that.editStoreAdsInfo.startTime = _value;
            });
            vc.initDateTime('editEndTime', function (_value) {
                $that.editStoreAdsInfo.endTime = _value;
            });
            vc.getDict('store_ads', "ad_type", function (_data) {
                $that.editStoreAdsInfo.adTypes = _data;
            });
        },
        _initEvent: function () {
            vc.on('editStoreAds', 'openEditStoreAdsModal', function (_params) {
                $that.clearEditStoreAdsInfo();
                $('#editStoreAdsModel').modal('show');

                vc.copyObject(_params, $that.editStoreAdsInfo);
                let _photos = [];
                _photos.push(_params.imgUrl);
                vc.emit('editStoreAds', 'uploadImageUrl', 'notifyPhotos', _photos);
                $that.editStoreAdsInfo.shopId = vc.getCurrentCommunity().shopId;
            });
            vc.on("editStoreAds", "notifyUploadCoverImage", function (_param) {
                if (_param.length > 0) {
                    $that.editStoreAdsInfo.imgUrl = _param[0].url;
                } else {
                    $that.editStoreAdsInfo.imgUrl = '';
                }
            });
        },
        methods: {
            editStoreAdsValidate() {
                return vc.validate.validate({
                    editStoreAdsInfo: $that.editStoreAdsInfo
                }, {
                    'editStoreAdsInfo.adName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "广告名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "128",
                            errInfo: "广告名称不能超过128位"
                        },
                    ],
                    'editStoreAdsInfo.adTypeCd': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "广告类型不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "12",
                            errInfo: "广告类型格式错误"
                        },
                    ],
                    'editStoreAdsInfo.seq': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "顺序不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "顺序不是正整数"
                        },
                    ],
                    'editStoreAdsInfo.startTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "开始时间不能为空"
                        },
                        {
                            limit: "dateTime",
                            param: "",
                            errInfo: "开始时间不是有效时间"
                        },
                    ],
                    'editStoreAdsInfo.endTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "结束时间不能为空"
                        },
                        {
                            limit: "dateTime",
                            param: "",
                            errInfo: "结束时间不是有效时间"
                        },
                    ],
                    'editStoreAdsInfo.pageUrl': [
                        {
                            limit: "maxLength",
                            param: "200",
                            errInfo: "链接地址超过200位"
                        },
                    ]
                });
            },
            editStoreAds: function () {
                if (!$that.editStoreAdsValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.editStoreAdsInfo.shopId = vc.getCurrentCommunity().shopId;

                vc.http.apiPost(
                    '/mallAd.updateMallAd',
                    JSON.stringify($that.editStoreAdsInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#editStoreAdsModel').modal('hide');
                            $that.clearEditStoreAdsInfo();
                            vc.emit('storeAdsManage', 'listStoreAds', {});
                            vc.toast("发布成功");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearEditStoreAdsInfo: function () {
                let _adTypes = $that.editStoreAdsInfo.adTypes;
                $that.editStoreAdsInfo = {
                    adId: '',
                    adName: '',
                    adTypeCd: '',
                    seq: '',
                    startTime: '',
                    endTime: '',
                    pageUrl: '',
                    imgUrl: '',
                    adTypes: _adTypes
                };
            }
        }
    });
})(window.vc);
